package jt.classic.system.workflow.impl;

import jt.classic.system.workflow.WFlow;
import jt.classic.system.workflow.WObject;
import jt.classic.system.workflow.WStep;
import jt.classic.system.workflow.WTrend;

import org.limp.basework.AbstractSimpleBean;

import org.limp.mine.StringTool;

import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * 流程接口实现类
 *
 * @author bizkit
 *
 */
public class WFlowImpl extends AbstractSimpleBean implements WFlow {
    private String id;
    private String key;
    private String name;
    private String memo;
    private String xmlscript;
    private List list;
    private WObject obj;
    private int maxstepno;
    private List steps;
    private String type = CUSTOM;

    public String id() {
        // TODO Auto-generated method stub
        return this.id;
    }

    public String ikey() {
        return this.key;
    }

    public String imemo() {
        // TODO Auto-generated method stub
        return this.memo;
    }

    public String iname() {
        // TODO Auto-generated method stub
        return this.name;
    }

    public String itype() {
        return this.type;
    }

    public boolean isfree() {
        return this.type.equals(FREE);
    }

    public WObject iobject() {
        // TODO Auto-generated method stub
        return this.obj;
    }

    public List iobjects() {
        // TODO Auto-generated method stub
        return this.list;
    }

    public int imaxstepno() {
        return maxstepno;
    }

    public List isteps() {
        return this.steps;
    }

    public WStep istep(String id) {
        WStep step = null;

        if ((this.steps != null) && !this.steps.isEmpty()) {
            for (int i = 0; i < this.steps.size(); i++) {
                WStep temp = (WStep) this.steps.get(i);

                if (temp.id().equals(id)) {
                    step = temp;

                    break;
                }
            }
        }

        return step;
    }

    public WStep istep4start() {
        WStep step = null;

        if ((this.steps != null) && !this.steps.isEmpty()) {
            for (int i = 0; i < this.steps.size(); i++) {
                WStep temp = (WStep) this.steps.get(i);

                if (temp.itype() == 0) {
                    step = temp;

                    break;
                }
            }
        }

        return step;
    }

    public WStep istep4end() {
        WStep step = null;

        if ((this.steps != null) && !this.steps.isEmpty()) {
            for (int i = 0; i < this.steps.size(); i++) {
                WStep temp = (WStep) this.steps.get(i);

                if (temp.itype() == -1) {
                    step = temp;

                    break;
                }
            }
        }

        return step;
    }

    public String toXML(String type) {
        StringBuffer xml = new StringBuffer();
        xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        xml.append("<WorkflowProcess>\n");

        if ((this.steps != null) && !this.steps.isEmpty()) {
            //Random random = new Random();
            xml.append("<Activities>\n");

            for (int i = 0; i < this.steps.size(); i++) {
                WStep step = (WStep) this.steps.get(i);

                int xCoordinate = (step.ixCoordinate() == 0) ? 10
                                                             : step.ixCoordinate();
                int yCoordinate = (step.iyCoordinate() == 0) ? 10
                                                             : step.iyCoordinate();

                xml.append("<Activitie id=\"" + step.ino() + "\" ");

                if (step.itype() == 0) {
                    xml.append(" type=\"START_NODE\" ");
                } else if (step.itype() == -1) {
                    xml.append(" type=\"END_NODE\" ");
                } else {
                    if (step.ifork()) {
                        xml.append(" type=\"FORK_NODE\" ");
                    } else if (step.ijion()) {
                        xml.append(" type=\"JOIN_NODE\" ");
                    } else {
                        xml.append(" type=\"NODE\" ");
                    }
                }

                xml.append(" name=\"" + step.iname() + "\" xCoordinate=\"" +
                    xCoordinate + "\" yCoordinate=\"" + yCoordinate + "\" ");
                xml.append("width=\"80\" height=\"30\"></Activitie>\n");
            }

            xml.append("</Activities>\n");

            xml.append("<Transitions>\n");

            for (int i = 0; i < this.steps.size(); i++) {
                WStep step = (WStep) this.steps.get(i);

                List trends = step.itrend();

                if ((trends != null) && !trends.isEmpty()) {
                    for (int j = 0; j < trends.size(); j++) {
                        WTrend trend = (WTrend) trends.get(j);
                        xml.append("<Transition id=\"" + (i) + "\" name=\"" +
                            trend.itrendname() + "\" from=\"" + step.ino() +
                            "\" to=\"" + trend.itrendno() +
                            "\"></Transition>\n");
                    }
                }
            }

            xml.append("</Transitions>\n");
        }

        xml.append("</WorkflowProcess>");

        this.xmlscript = xml.toString();

        return this.xmlscript;
    }

    public boolean isEmpty() {
        return (this.steps == null) || this.steps.isEmpty();
    }

    public void reload(Map info) {
        if (info != null) {
            this.info = info;

            this.id = (String) this.info.get("CG_ID");
            this.key = (String) this.info.get("CG_KEY");
            this.name = (String) this.info.get("CG_NAME");
            this.memo = (String) this.info.get("CG_MEMO");

            if (this.info.containsKey("CG_TYPE")) {
                this.type = (String) this.info.get("CG_TYPE");
            }

            this.obj = (WObject) this.info.get("OBJECT");
            this.list = (List) this.info.get("OBJECTS");

            if (this.info.containsKey("MAXSTEPNO")) {
                this.maxstepno = Integer.parseInt((String) this.info.get(
                            "MAXSTEPNO"));
            }

            this.steps = (List) this.info.get("STEPS");
        }
    }
}
